package shinesend.com.cn.mediaplayerdemo;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by MyPC on 2016/11/10.
 * MediaPlayer 的入门,和声音池的使用,应用在
 */
public class SecondActivity extends Activity implements View.OnClickListener{
    private MediaPlayer bgMediaPlayer;
    private MediaPlayer shoot1MP;
    private MediaPlayer shoot2MP;
    private MediaPlayer shoot3MP;
    private SoundPool sp;
    private int shoot1id;
    private int shoot2id;
    private int shoot3id;

    private Button play_bg;
    //使用mediaplayeer 播放的;
    private Button btn_mp_play_shoot1;
    private Button btn_mp_play_shoot2;
    private Button btn_mp_play_shoot3;
    //使用声音池播放的
    private Button btn_sp_play_shoot1;
    private Button btn_sp_play_shoot2;
    private Button btn_sp_play_shoot3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        findView();
        init();
    }

    private void findView(){
        play_bg = (Button) findViewById(R.id.btn_play_bg_music);
        play_bg.setOnClickListener(this);
        btn_mp_play_shoot1 = (Button) findViewById(R.id.btn_mp_play_shoot1);
        btn_mp_play_shoot1.setOnClickListener(this);
        btn_mp_play_shoot2 = (Button) findViewById(R.id.btn_mp_play_shoot2);
        btn_mp_play_shoot2.setOnClickListener(this);
        btn_mp_play_shoot3 = (Button) findViewById(R.id.btn_mp_play_shoot3);
        btn_mp_play_shoot3.setOnClickListener(this);
        btn_sp_play_shoot1 = (Button) findViewById(R.id.btn_sp_play_shoot1);
        btn_sp_play_shoot1.setOnClickListener(this);
        btn_sp_play_shoot2 = (Button) findViewById(R.id.btn_sp_play_shoot1);
        btn_sp_play_shoot2.setOnClickListener(this);
        btn_sp_play_shoot3 = (Button) findViewById(R.id.btn_sp_play_shoot3);
        btn_sp_play_shoot3.setOnClickListener(this);
    }

    private void init(){
        //MediaPlayer只能播放完一个音乐才能播放下一个音乐,SoundPool声音池是可以连续的;
        bgMediaPlayer = MediaPlayer.create(this,R.raw.jam);
        // 使用MediaPlayer播放短声音
        shoot1MP = MediaPlayer.create(this, R.raw.shoot1);
        shoot2MP = MediaPlayer.create(this, R.raw.shoot2);
        shoot3MP = MediaPlayer.create(this, R.raw.shoot3);

        // 使用SoundPool加载短声音
        // 3-->初始化声音池的实例对象, 最多可以加载3个声音
        //AudioManager.STREAM_MUSIC-->游戏的声音
        sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        // 加载声音
        shoot1id = sp.load(this, R.raw.shoot1, 1);
        shoot2id = sp.load(this, R.raw.shoot2, 1);
        shoot3id = sp.load(this, R.raw.shoot3, 1);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play_bg_music:
                bgMediaPlayer.start();
                break;
            case R.id.btn_mp_play_shoot1:
                shoot1MP.start();
                break;
            case R.id.btn_mp_play_shoot2:
                shoot2MP.start();
                break;
            case R.id.btn_mp_play_shoot3:
                shoot3MP.start();
                break;
            case R.id.btn_sp_play_shoot1:
                sp.play(shoot1id, 0.5f, 0.5f, 0, 0, 1.0f);
                break;
            case R.id.btn_sp_play_shoot2:
                sp.play(shoot2id, 0.5f, 0.5f, 0, 0, 1.0f);
                break;
            case R.id.btn_sp_play_shoot3:
                sp.play(shoot3id, 0.5f, 0.5f, 0, 0, 1.0f);
                break;

        }
    }
}
